
import 'package:event_bus/event_bus.dart';
typedef void EventCallback(arg);

//事件总线
class CnEventBus {
  factory CnEventBus() => _getInstance()!;

  static CnEventBus? get instance => _getInstance();
  static CnEventBus? _instance;

  CnEventBus._internal() {
    // 初始化
  }

  static CnEventBus? _getInstance() {
    if (_instance == null) {
      _instance = new CnEventBus._internal();
    }
    return _instance;
  }

  Map<String, EventCallback> _events = new Map();

  //设置事件监听，当有人调用 commit ，并且 eventKey 一样的时候会触发此方法
  void addListener(String? eventKey, EventCallback? callback) {
    if (eventKey == null || callback == null) return;
    _events[eventKey] = callback;
  }

  //移除监听
  void removeListener(String eventKey) {
    if (eventKey == null) return;
    _events.remove(eventKey);
  }

  //提交事件
  void commit(String eventKey, Object? arg) {
    if (eventKey == null) return;
    EventCallback? callback = _events[eventKey];
    if (callback != null) {
      callback(arg);
    }
  }
}

class EventKeys {
  static const String applePaySuccess = "applePaySuccess";
  static const String PaySuccess = "PaySuccess";
  static const String BurnMessage = "BurnMessage";
  static const String RongIMClientConnected = "RongIMClientConnected";
  static const String ConversationPageDispose = "ConversationPageDispose";
  static const String ReceiveMessage = "ReceiveMessage";
  static const String HomeListPageMessage = "HomeListPageMessage";
  static const String FollowListPageMessage = "FollowListPageMessage";
  static const String onTokenExpireCode = "onTokenExpireCode"; //Token失效消息
}
